<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <title>构建linux调试环境 | zsdfbb的博客</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <meta name="description" content="构建一个基于 qemu/gdb/arm64/linux6.2 的调试环境">
    <meta name="generator" content="Hugo 0.109.0">
    
    
    
    
      <meta name="robots" content="noindex, nofollow">
    

    
<link rel="stylesheet" href="/ananke/css/main.min.css" >



    
    
    
      

    

    
    
    <meta property="og:title" content="构建linux调试环境" />
<meta property="og:description" content="构建一个基于 qemu/gdb/arm64/linux6.2 的调试环境" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://zsdfbb.gitee.io/posts/qemu_debug_linux/" /><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2023-03-04T18:02:53+08:00" />
<meta property="article:modified_time" content="2023-03-04T18:02:53+08:00" />
<meta itemprop="name" content="构建linux调试环境">
<meta itemprop="description" content="构建一个基于 qemu/gdb/arm64/linux6.2 的调试环境"><meta itemprop="datePublished" content="2023-03-04T18:02:53+08:00" />
<meta itemprop="dateModified" content="2023-03-04T18:02:53+08:00" />
<meta itemprop="wordCount" content="544">
<meta itemprop="keywords" content="linux,debug,qemu,arm," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="构建linux调试环境"/>
<meta name="twitter:description" content="构建一个基于 qemu/gdb/arm64/linux6.2 的调试环境"/>

	
  </head>

  <body class="ma0 avenir bg-near-white">

    
   
  

  
  <header class="cover bg-top" style="background-image: url('https://zsdfbb.gitee.io/images/qemu_debug_linux.jpg');">
    <div class="bg-black-60">
      <nav class="pv3 ph3 ph4-ns" role="navigation">
  <div class="flex-l justify-between items-center center">
    <a href="/" class="f3 fw2 hover-white no-underline white-90 dib">
      
        zsdfbb的博客
      
    </a>
    <div class="flex-l items-center">
      

      
      
<div class="ananke-socials">
  
</div>

    </div>
  </div>
</nav>

      <div class="tc-l pv6 ph3 ph4-ns">
        
          <div class="f2 f1-l fw2 white-90 mb0 lh-title">构建linux调试环境</div>
          
            <div class="fw1 f5 f3-l white-80 measure-wide-l center lh-copy mt3 mb4">
              构建一个基于 qemu/gdb/arm64/linux6.2 的调试环境
            </div>
          
        
      </div>
    </div>
  </header>



    <main class="pb7" role="main">
      
  
  <article class="flex-l flex-wrap justify-between mw8 center ph3">
    <header class="mt4 w-100">
      <aside class="instapaper_ignoref b helvetica tracked">
          
        POSTS
      </aside>
      










  <div id="sharing" class="mt3 ananke-socials">
    
  </div>


      <h1 class="f1 athelas mt3 mb1">构建linux调试环境</h1>
      
      <p class="tracked">
        By <strong>zsdfbb</strong>
      </p>
      
      
      
      <time class="f6 mv4 dib tracked" datetime="2023-03-04T18:02:53+08:00">March 4, 2023</time>
      

      
      
    </header>
    <div class="nested-copy-line-height lh-copy serif f4 nested-links mid-gray pr4-l w-two-thirds-l"><h1 id="构建linux调试环境">构建linux调试环境</h1>
<p>由于研究linux内核的需要，我需要一个基于qemu/gdb/aarch64/linux 6.2 的调试环境。
这个是我部署的环境，仅供参考。</p>
<h2 id="我的开发环境">我的开发环境</h2>
<p>我使用 ubuntu 20.04 作为开发环境。同时我安装了下列程序，用于linux、busybox的编译、调试。</p>
<pre><code>sudo apt update -y &amp;&amp; sudo apt upgrade -y
sudo apt install qemu-user qemu-user-static gcc-aarch64-linux-gnu 
sudo apt install binutils-aarch64-linux-gnu
sudo apt install binutils-aarch64-linux-gnu-dbg
sudo apt install build-essential libncurses5-dev 
sudo apt install git bison flex libssl-dev
sudo apt install gdb gdb-multiarch
</code></pre>
<h2 id="我的目录结构">我的目录结构</h2>
<p>我的目录组织如下所示下面一些脚本配置个这些目录结构有关</p>
<pre tabindex="0"><code>[17:32:20] [~/Develop/LinuxLab]
-&gt; % tree -L 2
.
├── busybox
│   ├── build
│   ├── build.sh
│   ├── install
│   └── src
├── linux
│   ├── build
│   ├── build.sh
│   ├── install
│   └── src
├── mkrootfs.sh
├── qemu.run
└── rootfs_ext4.img
</code></pre><h2 id="编译linux">编译linux</h2>
<p>我使用 v6.2-rc7 版本的linux kernel。获取源码, &ndash;branch 指定了tag。</p>
<pre><code>git clone --branch v6.2-rc7 --depth=1 git@gitee.com:mirrors/linux_old1.git
</code></pre>
<p>使用下面的脚本编译（称为 build.sh）</p>
<p>例如:<br>
./build.sh config<br>
./build.sh build</p>
<pre><code>#! /bin/bash

WORK_DIR=${PWD}
SRC=$WORK_DIR/src
BUILD=$WORK_DIR/build

export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

if [ ! -d $BUILD ];then 
    mkdir ./build
fi

if [ $1 = &quot;config&quot; ]; then
    cd $BUILD
    make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=./ defconfig -f $SRC/Makefile
fi

if [ $1 = &quot;build&quot; ]; then
    cd $BUILD
    make -j16 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
fi

if [ $1 = &quot;clean&quot; ]; then
    cd $BUILD
    make clean
fi
</code></pre>
<p>使用 &ldquo;build.sh config&rdquo; 时调整下配置：</p>
<pre><code># ramdisk的大小
General setup  ---&gt;
    [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support

Device Drivers  ---&gt;
    [*] Block devices  ---&gt;
        &lt;*&gt;   RAM block device support
        (65536) Default RAM disk size (kbytes)
# 关闭 “地址随机化”：
Processor type and features
    -&gt; Build a relocatable kernel
        -&gt; Randomize the address of the kernel image (KASLR) (RANDOMIZE_BASE [=n])
# 关闭 “减少debug信息”
Kernel hacking -
    &gt; Compile-time checks and compiler options 
        &gt; Reduce debugging information
# 开启 “gdb 调试脚本”
Kernel hacking 
    &gt; Compile-time checks and compiler options 
        &gt; Provide GDB scripts for kernel debugging                                                
</code></pre>
<h2 id="编译busybox">编译busybox</h2>
<p>我使用下面的脚本编译busybox（称为 build.sh）。可以按照下面的方式进行配置、编译。</p>
<p>例如:<br>
./build.sh config<br>
./build.sh build<br>
./build.sh install</p>
<pre><code>#! /bin/bash

# 配置
WORK_DIR=/home/shuai/LinuxLab/busybox
SRC=$WORK_DIR/src
BUILD=$WORK_DIR/build
INSTALL=$WORK_DIR/install

# 执行
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

if [ ! -d $BUILD ];then 
    mkdir ./build
fi

if [ ! -d $INSTALL ];then 
    mkdir ./install
fi

if [ $1 = &quot;config&quot; ]; then
    cd $BUILD
    make KBUILD_SRC=$SRC -f $SRC/Makefile defconfig
    make menuconfig
fi

if [ $1 = &quot;build&quot; ]; then
    cd $BUILD
    make -j16 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
fi

if [ $1 = &quot;install&quot; ]; then
    cd $BUILD
    make -j16 CONFIG_PREFIX=${INSTALL} install
fi

if [ $1 = &quot;clean&quot; ]; then
    cd $BUILD
    make clean
fi
</code></pre>
<p>使用 &ldquo;build.sh config&rdquo; 时调整下配置：</p>
<pre tabindex="0"><code>Build Options ---&gt; [*] Build BusyBox as a static binary (no shared libs)
</code></pre><h2 id="构建rootfs">构建rootfs</h2>
<p>mkrootfs.sh 构建rootfs的脚本：</p>
<pre><code>#!/bin/bash

# 配置
IMAGE_FILE=rootfs_ext4.img
BUSYBOX_DIR=./busybox

# 执行
if [ -e $IMAGE_FILE ];then
    rm $IMAGE_FILE
fi

dd if=/dev/zero of=$IMAGE_FILE bs=1M count=256 oflag=direct
mkfs.ext4 $IMAGE_FILE

mkdir rootfs
sudo mount $IMAGE_FILE rootfs/
sudo cp -raf $BUSYBOX_DIR/install/* rootfs/

cd rootfs
sudo mkdir -p proc sys tmp root var mnt dev
sudo mknod dev/tty1 c 4 1
sudo mknod dev/tty2 c 4 2
sudo mknod dev/tty3 c 4 3
sudo mknod dev/tty4 c 4 4
sudo mknod dev/console c 5 1
sudo mknod dev/null c 1 3
sudo cp -r ../$BUSYBOX_DIR/src/examples/bootfloppy/etc/ .

cd ..
sudo umount rootfs
rmdir rootfs
</code></pre>
<h2 id="qemu的启动脚本">qemu的启动脚本</h2>
<p>我使用 ./qemu.run 拉起qemu调试环境：</p>
<pre tabindex="0"><code>#!/bin/bash
qemu-system-aarch64 \
        -kernel ./linux/build/arch/arm64/boot/Image \
        -nographic \
        -append &#34;root=/dev/vda console=ttyAMA0 rootfstype=ext4 init=/linuxrc rw&#34; \
        -m 2048M \
        -smp 4  \
        -M virt \
        -cpu cortex-a57 \
        -hda rootfs_ext4.img
</code></pre><p>启动日志：</p>
<pre tabindex="0"><code>[ 0.561507] Run /linuxrc as init process

Processing /etc/profile... Done

~ # ls
bin linuxrc proc sys var dev lost+found root tmp etc mnt sbin usr
~ #
</code></pre><ul class="pa0">
  
   <li class="list di">
     <a href="/tags/linux/" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">linux</a>
   </li>
  
   <li class="list di">
     <a href="/tags/debug/" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">debug</a>
   </li>
  
   <li class="list di">
     <a href="/tags/qemu/" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">qemu</a>
   </li>
  
   <li class="list di">
     <a href="/tags/arm/" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">arm</a>
   </li>
  
</ul>
<div class="mt6 instapaper_ignoref">
      
      
      </div>
    </div>

    <aside class="w-30-l mt6-l">




</aside>

  </article>

    </main>
    <footer class="bg-black bottom-0 w-100 pa3" role="contentinfo">
  <div class="flex justify-between">
  <a class="f4 fw4 hover-white no-underline white-70 dn dib-ns pv2 ph3" href="https://zsdfbb.gitee.io/" >
    &copy;  zsdfbb的博客 2023 
  </a>
    <div>
<div class="ananke-socials">
  
</div>
</div>
  </div>
</footer>

  </body>
</html>
